package com.flux.tms.tm${bean.numMain}srv.t${bean.numMain}${bean.numSub}.impl.ctrl;

import static org.mockito.ArgumentMatchers.any;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.Test;

import com.flux.tms.tm${bean.numMain}srv.common.ctrl.CommonCtrl;

import com.flux.tms.tm${bean.numMain}srv.common.ctrl.CommonCtrl;
import com.flux.tms.tm${bean.numMain}srv.t${bean.numMain}${bean.numSub}.${bean.cvalName};
<#if (bean.importSer)?size !=0>
    <#list (bean.importSer)! as ipDao><#if (ipDao.name)?contains("Ctrl")>
import com.flux.tms.tm${bean.numMain}srv.t${bean.numMain}${bean.numSub}.impl.ctrl.${ipDao.name};
    <#else >
import com.flux.tms.tm${bean.numMain}srv.t${bean.numMain}${bean.numSub}.impl.service.${ipDao.name};
    </#if>
    </#list>
</#if>
import com.flux.wmsv5.comobjs.CommonCVal;
import com.flux.wmsv5.comobjs.LoginInfoObj;
import com.flux.wmsv5.utils.LoginInfoUtil;


/**
*
* 单元测试类:  ${bean.fileName}
*
* 创建时间：${bean.dayTime}
*
* 修改时间：${bean.dayTime}
*
* @author YangSheng
*
* @version V1.0.0
*
*/
public class ${bean.fileName}Test extends CommonCtrl{
    @InjectMocks
    private ${bean.fileName} ${bean.fileName};
    <#if (bean.importSer)?size !=0>
        <#list 0..((bean.importSer)?size-1) as i><#assign dao=(bean.importSer)[((bean.importSer)?size-1)].name2>
            <#if ((bean.importSer)?size>=2) ><#assign dao2=(bean.importSer)[1].name2></#if>
    @Mock
    ${(bean.importSer)[i].desc};
        </#list>
    </#if>

    @Override
    protected CommonCtrl returnSubClass() {
        return this;
    }

    @Override
    protected void initSubLogin() {

    }

    @Override
    protected void initSubBizDataMap() {
        String[] wantCheckIdsForBizData = { ${bean.wantCheckIdsForBizData}
        };
        for(String key : wantCheckIdsForBizData){
            bizDataMap.put(key, "123");
        }
    }

    @Test
    public void test() throws Exception {
        init();
        //测试登录对象无数据
        loginNoData();
        //测试业务数据字典无数据
        initLogin();
        bizDataNoData();
        initBizDataMap();
        reObj.setCodeValueOK();
        reObj.setMessageId("123");
        reObjErr.setCodeValueERR();
        reObjErr.setMessage("err");
        //测试其他
        testOther();
        //测试正常
        testOK();
    }

    @Override
    protected void loginNoDataSub(String loginInfo, String bizData) {
        ${bean.fileName} spy = PowerMockito.spy(${bean.fileName});
        spy.doAction(loginInfo, bizData);
        // 日志批次号校验
        loginInfoObj.setLogBatchNo("sf");
        String appSecret = appSecretService.getAppSecret();
        loginInfo = LoginInfoUtil.toJSONString(loginInfoObj, appSecret);
        spy.doAction(loginInfo, bizData);
    }

    @Override
    protected void bizDataNoDataSub(String loginInfo, String bizData) {
        ${bean.fileName} spy = PowerMockito.spy(${bean.fileName});
        spy.doAction(loginInfo, bizData);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void testOtherSub(String loginInfo, String bizData) throws Exception {
            ${bean.fileName} spy = PowerMockito.spy(${bean.fileName});
            Map<String, Object> dataMap = new HashMap<String, Object>();
            bizData = bizDataMapToString();
            spy.doAction(loginInfo, bizData);
            bizDataMap.put("organizationId", "$$$");
            bizData = bizDataMapToString();
            spy.doAction(loginInfo, bizData);

            bizDataMap.put("organizationId", "123");
            bizDataMap.put("dispatchZoneCode", "$$$");
            bizData = bizDataMapToString();
            spy.doAction(loginInfo, bizData);

            bizDataMap.put("organizationId", "FLUX");
            bizDataMap.put("dispatchZoneCode", "123");
            bizDataMap.put("activeFlag", "SSS");
            bizData = bizDataMapToString();
            spy.doAction(loginInfo, bizData);

            bizDataMap.put("organizationId", "FLUX");
            bizDataMap.put("dispatchZoneCode", "123");
            bizDataMap.put("activeFlag", "Y");
            bizData = bizDataMapToString();
            spy.doAction(loginInfo, bizData);

            // 前置报错
            PowerMockito.when(stdBizExtExecuteService.execute(any(LoginInfoObj.class), any(Map.class), any(String.class),
            any(String.class), Mockito.eq("PREOPERATION"))).thenReturn(reObjErr);
            spy.doAction(loginInfo, bizData);
            // 前置不报错，SQLcheck报错
            PowerMockito.when(stdBizExtExecuteService.execute(any(LoginInfoObj.class), any(Map.class), any(String.class),
            any(String.class), Mockito.eq("PREOPERATION"))).thenReturn(reObj);
            spy.doAction(loginInfo, bizData);


            // 查询是否存在

            dataMap.put("routeLineNo", "0");
          //  PowerMockito.doReturn(dataMap).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).getRulDisZone(any(LoginInfoObj.class),
          //  any(Map.class));
            spy.doAction(loginInfo, bizData);

         //   PowerMockito.doReturn(null).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).getRulDisZone(any(LoginInfoObj.class),
         //   any(Map.class));
            spy.doAction(loginInfo, bizData);

            <#--bizDataMap.put(${bean.pk05}CVal.KEYNAME_TOCOUNTRY, "toCountry");-->
            bizDataMap.put("organizationId", "toCountry");
            bizDataMap.put("dispatchZoneCode", "toCountry");
            bizDataMap.put("toCountry", "toCountry");
            bizDataMap.put("toCity", "toCity");
            bizDataMap.put("toProvince", "toProvince");
            bizDataMap.put("toDistrict", "toDistrict");
            bizDataMap.put("toStreet", "toStreet");
            bizDataMap.put("branchId", null);
            bizDataMap.put("activeFlag", "Y");
            bizData = bizDataMapToString();

         //   PowerMockito.doReturn(null).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).getRulDisZoneDetail(any(LoginInfoObj.class),any(Map.class));
         //   spy.doAction(loginInfo, bizData);
         //   PowerMockito.doReturn(dataMap).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).getRulDisZoneDetail(any(LoginInfoObj.class),any(Map.class));
         //   spy.doAction(loginInfo, bizData);
<#if (bean.importSer)?size !=0>
            // postDataWithExtend resultObj 返回 false
            // postData返回错误
            reObjErr.setCodeValueERR();
            PowerMockito.doReturn(reObjErr).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).postData(any(LoginInfoObj.class),
            any(Map.class));
            spy.doAction(loginInfo, bizData);

            // postData异常RowDataException
            PowerMockito.doThrow(RowDataException).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).postData(any(LoginInfoObj.class),
            any(Map.class));
            spy.doAction(loginInfo, bizData);
            //
            // //postData异常RowDataException
            PowerMockito.doThrow(exception).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).postData(any(LoginInfoObj.class),
            any(Map.class));
            spy.doAction(loginInfo, bizData);
            //
            // //postData返回正确,后置报错
            PowerMockito.doReturn(reObj).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).postData(any(LoginInfoObj.class), any(Map.class));
            spy.doAction(loginInfo, bizData);
</#if>
            PowerMockito.when(stdBizExtExecuteService.execute(any(LoginInfoObj.class), any(Map.class), any(String.class),
            any(String.class), Mockito.eq("POSTOPERATION"))).thenReturn(reObjErr);
            spy.doAction(loginInfo, bizData);
            //
            // 后置正确
            PowerMockito.when(stdBizExtExecuteService.execute(any(LoginInfoObj.class), any(Map.class), any(String.class),
            any(String.class), Mockito.eq("POSTOPERATION"))).thenReturn(reObj);
            spy.doAction(loginInfo, bizData);

            spy.doAction(null, bizData);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void testOKSub(String loginInfo, String bizData) throws Exception {
        initSub();
        ${bean.fileName} spy = PowerMockito.spy(${bean.fileName});
        // 自己开始写postData的测试，因为部分的postData的参数个数不一，注意参数的数量；
<#if (bean.importSer)?size !=0>
        PowerMockito.doReturn(reObj).when(<#if dao??>${dao}<#else ><#if dao2??>${dao2}</#if></#if>).postData(any(LoginInfoObj.class), any(Map.class));
</#if>  spy.doAction(loginInfo, bizData);
    }

    @Override
    protected void initSub() throws Exception {

    }

}
